package com.xiaoxie.web.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.github.pagehelper.PageInfo;
import com.xiaoxie.common.union.R;
import com.xiaoxie.system.domain.Ds;
import com.xiaoxie.system.domain.dto.DsDto;
import com.xiaoxie.system.domain.dto.DsQuery;
import com.xiaoxie.system.domain.dto.DsOptionDto;
import com.xiaoxie.system.service.DsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/system/ds")
@Tag(name = "数据源管理",description = "数据源管理")
public class DsController {


    @Resource
    private DsService dsService;

    @Operation(summary = "根据id获取数据源")
    @GetMapping("/{id}")
    public R getDsById(@PathVariable("id") Integer id) {
        Ds ds = dsService.getDsById(id);
        return ds != null ? R.ok(ds) : R.fail("获取数据源失败");
    }

    @Operation(summary = "获取数据源列表")
    @GetMapping("/list")
    public R list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, DsQuery ds) {
        PageInfo<Ds> pageInfo = dsService.selectByConditionWithPage(pageNum, ds);
        return R.ok(pageInfo);
    }

    @Operation(summary = "根据数据源名称查询是否存在")
    @GetMapping("/checkName")
    public R checkName(@RequestParam(value="name") String name) {
        return  R.ok(dsService.checkName(name));
    }

    @Operation(summary = "根据数据源名称查询更新时是否已存在")
    @GetMapping("/updateCheckName")
    public R updateCheckName(@RequestParam(value="name") String name,@RequestParam(value = "id") Integer id) {
        return  R.ok(dsService.updateCheckName(name,id));
    }

    @Operation(summary = "测试数据源连接")
    @PostMapping("/testConnection")
    public R testConnection(@RequestBody DsDto dsDto) {
        Boolean result = dsService.testConnection(dsDto);
        return result ? R.ok("测试连接成功") : R.ok("测试连接失败");
    }

    @Operation(summary = "添加数据源")
    @SaCheckPermission("datasource:add")
    @PostMapping("/add")
    public R AddDs(@RequestBody DsDto dsDto) {
        boolean result = dsService.addDs(dsDto);
        return result ? R.ok("添加数据源成功") : R.fail("添加数据源失败");
    }

    @Operation(summary = "获取所有数据源")
    @GetMapping("/sources")
    public R getDataSources() {
        Set<Object> result = dsService.getDataSources();
        return result != null ? R.ok(result) : R.fail("获取数据源失败");
    }

    @Operation(summary = "更新数据源")
    @PutMapping("/update")
    public R updateDs(@RequestBody DsDto dsDto) {
        dsService.updateDs(dsDto);
        return R.ok("更新数据源成功");
    }

    @Operation(summary = "根据id删除数据源")
    @DeleteMapping("/{id}")
    public R deleteDsById(@PathVariable("id") Integer id) {
        dsService.deleteDsById(id);
        return R.ok("删除数据源成功");
    }

    @Operation(summary = "根据数据源名称删除数据源")
    @DeleteMapping("/deleteByName")
    public R deleteDsByDsName(@RequestParam("dsName") String dsName) {
        dsService.deleteDsByDsName(dsName);
        return R.ok("删除数据源成功");
    }

    @Operation(summary = "批量删除数据源")
    @DeleteMapping("/batchDel")
    public R batchDeleteDs(@RequestParam("ids") String ids) {
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .toList();
        dsService.batchDeleteDsById(idList);
        return R.ok("删除成功");
    }

    @Operation(summary = "查询所有数据源名称及id")
    @GetMapping("/getDsOptions")
    public R getDsOptions() {
        List<DsOptionDto> result = dsService.getDsOptions();
        return R.ok(result);
    }
}
